package cn.merryyou.jpa.domain;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Created on 2018/2/5.
 *
 * @author zlf
 * @since 1.0
 */
@Entity
@Setter
@Getter
@Table(name = "publisher")
@NoArgsConstructor
@Slf4j
public class Publisher implements Serializable {

    private static final long serialVersionUID = -571215472498509365L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    @OneToMany(mappedBy = "publisher",cascade = CascadeType.ALL)
    private Set<BookPublisher> bookPublishers;

    public Publisher(String name){
        this.name = name;
        bookPublishers = new HashSet<>();
    }

    @Override
    public String toString() {
        String result = String.format(
                "Publisher [id=%d, name='%s']%n",
                id, name);
        if (bookPublishers != null) {
            log.info(JSON.toJSONString(bookPublishers));
        }

        return result;
    }

}
